/*
 * SubjectsDao.java
 *
 * Created on November 9, 2008, 7:40 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.Subjects;

/**
 *
 * @author JollkY
 */
public class SubjectsDao implements Crud {
    
    private int state;
    private Subjects subject;
    private List subjects;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();
    
    /** Creates a new instance of SubjectsDao */
    public SubjectsDao() {
    }

    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    public List<Object> getList() {
        subjects = new ArrayList();
        try {
            em.getTransaction().begin();
            subjects = em.createQuery("SELECT s FROM Subjects s").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return subjects;
    }

    public Object read(long id) {
        subject = new Subjects();
        try {
            em.getTransaction().begin();
            subject = em.find(Subjects.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return subject;
    }

    public int update(Object obj) {
        subject = new Subjects();
        try {
            Subjects objSbj = (Subjects) obj;
            em.getTransaction().begin();
            subject = em.find(Subjects.class, objSbj.getId());
            subject.setDescription(objSbj.getDescription());
            subject.setName(objSbj.getName());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    public int delete(long id) {
        subject = new Subjects();
        try {
            em.getTransaction().begin();
            subject = em.find(Subjects.class, id);
            em.remove(subject);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }
    
}
